﻿using System.Security.Principal;
using KL.UFrame.Core.Exception;
using KL.UFrame.Core.Unity;
using KL.UFrame.Data.Business.Account;
using KL.UFrame.Data.Models.Account;

namespace KL.UFrame.Api.Extension
{
    /// <summary>
    /// 对Controller中的User进行扩展
    /// By：杨超
    /// </summary>
    public  static class IPrincipleExtension
    {
        /// <summary>
        /// 获取当前用户的信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public static async System.Threading.Tasks.Task<UserInfoModel> GetUserInfoAsync(this IPrincipal user)
        {
            if (string.IsNullOrEmpty(user.Identity.Name))
            {
                throw new ResultException(-403, "用户未认证，无法获取用户信息");
            }

            var accountBusiness = UnityBootstrapper.Current.GetService<IAccountBusiness>();

            return await accountBusiness.GetUserInfoByNameAsync(user.Identity.Name);
        }
    }
}